package com.xinghou.platform.server.api.bussiness.impl;

import com.xinghou.platform.dao.main.UserDAO;
import com.xinghou.platform.dao.main.UserInviteDAO;
import com.xinghou.platform.entity.req.ListReq;
import com.xinghou.platform.entity.req.UserInviteReq;
import com.xinghou.platform.entity.resp.UserInfoResp;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.model.main.UserInvite;
import com.xinghou.platform.server.api.bussiness.UserInviteService;
import com.xinghou.platform.server.api.common.Constants;
import com.xinghou.platform.server.api.common.ImageComb;
import com.xinghou.platform.util.BigWheelDrawUtil;
import com.xinghou.platform.util.PhotoPathUtil;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 16/9/9.
 */
@Service("userInviteService")
public class UserInviteServiceImpl implements UserInviteService {

    private static int SOCRE_PER_DRAW = 100;
    @Resource
    public UserInviteDAO userInviteDAO;

    @Resource
    public UserDAO userDAO;

    @Override
    public void  drawPrize(Integer uid) {

    }

    @Override
    public boolean canDrawPrize(Integer uid) {
     return false;
    }

    @Override
    public int drawResult(Integer uid) {
        return BigWheelDrawUtil.generateAward().getPricelevel();
    }
    @Override
    public String getInviteCodeByUser(Integer userid){
        if(userid <7000000){
            throw new ServiceException(DBExceptionEnums.PRARM_ERROR);
        }
        if(userid <10000000&&userid>9200000) {
            return "0"+String.valueOf(userid - 9200000);
        }
        return String.valueOf(userid - Constants.INVITE);
    }

    @Override
    public Integer getUserByInviteCode(String invite){
        Integer inviteNum = Integer.parseInt(invite);
        if(inviteNum<100000){
            return inviteNum + 9200000;
        }
        return inviteNum + Constants.INVITE;

    }

    @Override
    public void addUserInvite(UserInvite userInvite){
        userInviteDAO.addUserInvite(userInvite);
    }

    @Override
    public List<UserInfoResp> getRank(Integer userid){
        List<UserInvite> list = userInviteDAO.getUserInviteRank(userid);
        List<UserInfoResp>  resultList= new ArrayList<UserInfoResp>();
        for(UserInvite userInvite :list) {
            UserInfoResp uR = new UserInfoResp();
            uR.setUserid(String.valueOf(userInvite.getInviteuid()));
            User user =  userDAO.getUserById(userInvite.getInviteuid());
            if(user ==null){
                continue;
            }
            uR.setIndex(String.valueOf(userInvite.getIndex()));
            uR.setHeadurl(PhotoPathUtil.getHeader50w50h(ImageComb.combHeaderUrl(user.getHeadurl())));
            uR.setNickname(user.getNickname());
            uR.setScore(String.valueOf(userInvite.getScore()));
            resultList.add(uR);
        }
        return resultList;
    }

    @Override
    public List<UserInfoResp> getInvitedUser(ListReq listReq){
        List<UserInvite> list = userInviteDAO.getInvitedUser(listReq);
        List<UserInfoResp>  resultList= new ArrayList<UserInfoResp>();
        for(UserInvite userInvite :list) {
            UserInfoResp uR = new UserInfoResp();
            uR.setUserid(String.valueOf(userInvite.getUid()));
            User user =  userDAO.getUserById(userInvite.getUid());
            if(user ==null){
                continue;
            }
            uR.setCreatetime(String.valueOf(userInvite.getCreatetime()));
            uR.setNickname(user.getNickname());
            uR.setHeadurl(PhotoPathUtil.getHeader50w50h(ImageComb.combHeaderUrl(user.getHeadurl())));
            uR.setScore(String.valueOf(userInvite.getScore()));
            resultList.add(uR);
        }
        return resultList;
    }

    @Override
    public UserInfoResp getIndexAndScoreByUser(Integer userid){
        UserInfoResp uR = new  UserInfoResp();
        UserInvite userInvite = userInviteDAO.getIndexAndScoreByUser(userid);
        User user =  userDAO.getUserById(userid);
        uR.setIndex(String.valueOf(userInvite.getIndex()==null?0:userInvite.getIndex()));
        uR.setScore(String.valueOf(userInvite.getScore()==null?0:userInvite.getScore()));
        uR.setNickname(user.getNickname());
        uR.setHeadurl(PhotoPathUtil.getHeader50w50h(ImageComb.combHeaderUrl(user.getHeadurl())));
        return uR;
    }

}
